﻿using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Drawing;
using System.Xml;
using System.Data;
using System.Collections;
using System.Web;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:TweetMeButton runat=server></{0}:TweetMeButton>")]
    public class TweetMeButton : WebControl
    {

        [Category("Service")]
        public string TweetMeScriptUrl
        {
            get
            {
                object obj = ViewState["TweetMeScriptUrl"];
                if (obj == null) { return "http://tweetmeme.com/i/scripts/button.js"; }
                return obj.ToString();
            }
            set { ViewState["TweetMeScriptUrl"] = value; }
        }

        [Category("Appearance")]
        public string Title
        {
            get
            {
                object obj = ViewState["Title"];
                if (obj == null) { return this.Page.Header.Title; }
                return obj.ToString();
            }
            set { ViewState["Title"] = value; }
        }

        
        [Category("Appearance")]
        public bool UseCompactButton
        {
            get
            {
                object obj = ViewState["UseCompactButton"];
                if (obj == null) { return false; }
                return Convert.ToBoolean(obj);
            }
            set { ViewState["UseCompactButton"] = value; }

        }

        [Category("Appearance")]
        public string DocumentUrl
        {
            get
            {
                object obj = ViewState["DocumentUrl"];
                if (obj == null) { return string.Empty; }
                return obj.ToString();
            }
            set { ViewState["DocumentUrl"] = value; }
        }

        [Category("Appearance")]
        public string TwitterUsername
        {
            get
            {
                object obj = ViewState["TwitterUsername"];
                if (obj == null) { return string.Empty; }
                return obj.ToString();
            }
            set { ViewState["TwitterUsername"] = value; }
        }


        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode) { base.Render(writer); return; }

            writer.AddAttribute("type", "text/javascript");
            writer.AddAttribute("language", "javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            if (this.UseCompactButton) { writer.WriteLine("tweetmeme_style = 'compact';"); }
            if (!string.IsNullOrEmpty(this.DocumentUrl)) { writer.WriteLine(string.Format("tweetmeme_url = '{0}';", this.DocumentUrl)); }
            if (!string.IsNullOrEmpty(this.TwitterUsername)) { writer.WriteLine(string.Format("tweetmeme_source = '{0}';", this.TwitterUsername)); }
            if (!string.IsNullOrEmpty(this.Title)) { writer.WriteLine(string.Format("tweetmeme_title = '{0}';", this.Title )); }
            writer.RenderEndTag();

            writer.AddAttribute("type", "text/javascript");
            writer.AddAttribute("language", "javascript");
            writer.AddAttribute("src", this.TweetMeScriptUrl);
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.RenderEndTag();
        }
    }
}
 